package cn.gailvlun.gll.chatkit;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.IOException;

import cn.gailvlun.gll.chatlib.message.Message;



public class PlayerService extends Service {

    private MediaPlayer mediaPlayer;

    private PlayerBinder mBinder = new PlayerBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("test", "playerService onCreate()");
    }

    @Override
    public void onDestroy() {
        Log.d("test", "playerService onDestroy()");
        super.onDestroy();
    }

    public void playServiceAudio(String file) {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        Log.d("test", "playerService playServiceAudio()");
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(file);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Message mCurrentVoiceMessage;

    public void playServiceAudio(final Message voiceMessage) {
        mCurrentVoiceMessage = voiceMessage;
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        Log.d("test", "playerService playServiceAudio()");
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(voiceMessage.getRealWalkieTalkie());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopServiceAudio() {
        Log.d("test", "playerService stopServiceAudio()");
        if (mediaPlayer != null) {
            if (mOnCompletionListener != null) {
                mOnCompletionListener.onCompletion(mediaPlayer);
                mOnCompletionListener = null;
            }
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public boolean isServicePlaying() {
        if (mediaPlayer != null) {
            return mediaPlayer.isPlaying();
        } else {
            return false;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    private void setServiceListener(MediaPlayer.OnCompletionListener listener) {
        if (mediaPlayer != null) {
            mOnCompletionListener = listener;
            mediaPlayer.setOnCompletionListener(listener);
        }
    }

    private MediaPlayer.OnCompletionListener mOnCompletionListener;

    public class PlayerBinder extends Binder {
        public void playAudio(String file) {
            playServiceAudio(file);
        }

        public void playAudio(Message voiceMessage) {
            playServiceAudio(voiceMessage);
        }

        public void stopAudio() {
            stopServiceAudio();
        }

        public Message getCurrentPlaying() {
            return mCurrentVoiceMessage;
        }

        public boolean isPlaying() {
            return isServicePlaying();
        }

        public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
            setServiceListener(listener);
        }
    }
}
